#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <iostream>

using namespace std;
using namespace cv;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    frame_to_picture();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::frame_to_picture()
{
    //using namespace cv;
    cv::VideoCapture cap;

    if(!cap.open("C:/Users/liudongpeng/Desktop/Qt/BadApple/badapple.mp4"))
    {
        qDebug("open error");
        return;
    }


    long total_frame_number = cap.get(CAP_PROP_FRAME_COUNT);
    cout << total_frame_number << endl;

    long frame_start = 1;
    cap.set(CAP_PROP_POS_FRAMES, frame_start);

    long frame_end = total_frame_number;

    Mat frame, dest, img;

    long frame_cur = frame_start;

    int picture_count = 0;

    while(frame_cur <= frame_end)
    {

        if(!cap.read(frame))
        {
            qDebug("read error");
            goto end;
        }


        //if(frame_cur % 2 == 0)
        {
            picture_count++;
            stringstream str;
            str << "C:/Users/liudongpeng/Desktop/Qt/BadApple/picture/" << picture_count << ".bmp";

            cout << str.str() << endl;
            cv::resize(frame, dest, Size(128, 64));
            imwrite(str.str(), dest);
        }


        frame_cur++;
    }

end:
    cout << picture_count << endl;

    cap.release();
    waitKey(0);
    return;
}
